<?php
/**
 * Created by PhpStorm.
 * User: yyq
 * Date: 2017/3/28
 * Time: 17:46
 */

namespace Wap\Controller;


use Common\Model\MemberModel;
use System\Model\SmsModel;
use Think\Controller;

class ShareController extends Controller
{
    private $member_model;
    private $sms_model;

    public function __construct()
    {
        parent::__construct();
        $this->member_model = new MemberModel();
        $this->sms_model = new SmsModel();
    }

    public function index() {

            $this->display();

    }

    public function sendSms() {
        $username = I('post.username');//dump($username);exit;
//        $this->checkparam([$username]);


//        if ($this->member_model->checkUserName($username) > 0)
//            exit($this->returnApiError(ApibaseController::FATAL_ERROR,
//                '该帐号已注册'
//            ));

        $code = get_code(6, 1);
        $content = '【'.C('SMS_ACCOUNT.company').'】你的验证码是:' . $code . '，15分钟内有效。';

        if (strlen($username) != 11)
            exit($this->returnApiError(ApibaseController::FATAL_ERROR, '手机号码错误'));

        vendor("Cxsms.Cxsms");
        $options = C('SMS_ACCOUNT');
        $Cxsms = new \Cxsms($options);
        $result = $Cxsms->send($username, $content);

        if ($result && $result['returnsms']['returnstatus'] != 'Success') {
            exit($this->returnApiError(ApibaseController::FATAL_ERROR, '验证码发送失败'));
        }


        if (!$this->sms_model->add_row($code, $content, $username))
            exit($this->returnApiError(ApibaseController::FATAL_ERROR, '写入失败'));

        exit($this->returnApiSuccess());
    }
}